package irri.soils.ssnm;

import android.content.Intent;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ActivityB extends SSNM implements TextWatcher{
	private Bundle b;
	private NoImeEditText ha;
	private Button ok;
	private DecimalFilter filter;
	private TextView question;
	private TextView textView1;
	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b);
        setCustomTitle();
        b = this.getIntent().getExtras();
        question = (TextView) findViewById(R.id.question);
        question.setText(get("FS0"));
        textView1 = (TextView) findViewById(R.id.textView1);
        textView1.setText(get("FS2"));
        
        ha = (NoImeEditText) findViewById(R.id.ha);
        if(b.containsKey("ha"))	ha.setText(""+df.format(b.getDouble("ha")));
        ha.performClick();
        filter = new DecimalFilter();
        ha.setKeyListener( filter );
        ha.addTextChangedListener(this);
        
        mInputView = (KeyboardView) findViewById(R.id.keyboard);
        
        mInputView.setOnKeyboardActionListener(this);
        mSymbolsKeyboard = new LatinKeyboard(this, R.xml.fraction);
        mInputView.setKeyboard(mSymbolsKeyboard);
    }
	
	@Override
	public void onBackPressed(){
		Intent intent = new Intent(ActivityB.this, ActivityMain.class);
		intent.putExtras(b);
		startActivity(intent);
		finish();
	}

	@Override
	public void afterTextChanged(Editable s) {
		String haTxt = ""+ha.getText();
		try{
			double haVal = haTxt.equals("") ? 0 : Double.parseDouble(haTxt);
			if(haVal>10){ 
				ha.setText(""); 
				ha.append(haTxt.substring(0, haTxt.length()-1));
			}
			if(haVal<0.1&&haVal!=0){
				ha.setText(""); 
				ha.append("0.1");
			}
			ok.setEnabled(!haTxt.equals("")&&haVal!=0);
		}catch(Exception ex){
			
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onKey(int primaryCode, int[] keyCodes) {
		View v = this.getCurrentFocus();
		try{
			TextView tv = (TextView) v;
			String str = ""+tv.getText();
			
			if (primaryCode == Keyboard.KEYCODE_DELETE) {
				onDelete(tv, str);
			}
			else if(primaryCode == 10){
				try{
				Intent intent = new Intent(ActivityB.this, ActivityZ.class);
				Double haVal = Double.parseDouble(""+ha.getText());
				if(haVal>=0.025&&haVal<=10){
					if(b.getDouble("ha")!=haVal){
						b.remove("cav");
						b.remove("cav2");
					}
					b.putDouble("ha", haVal);
					intent.putExtras(b);
					startActivity(intent);
					finish();
				}}catch(Exception ex){
					
				}
			}
			else{
				onDigit(tv, str, primaryCode);
			}
		}catch(Exception ex){
			
		}
	}
}